<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of grid_service
 *
 * @author Administrator
 */
class api_service extends MY_Service
{

    private $userdata;

    public function __construct()
    {
        parent::__construct();
        $this->load->model('wms/api/api_model');
        $this->load->model('wms/api/get_product_info_from_erp');
        $this->load->model('wms/warehouse_list_model');
        $this->load->model('wms/product_inventory_model');
        $this->userdata = array(
            'tomrepair.es' => 'omstomrepaires20161214',
        );
    }

    function checkAuthorized($postData)
    {
        $user = $postData['user'];
        $timestamp = $postData['timestamp'];
        $sign = $postData['sign'];
        $error = '';
        $isOk = false;
        if (!$this->userdata[$user])
        {
            $error = 'Unauthorized';
        }
        //hash签名。将用户名+时间戳+密码的字符进行MD5（MD5值为小写）//md5($user . $timestamp.$pass)
        $signRs = md5($user . $timestamp . $this->userdata[$user]);
        if ($sign == $signRs)
        {
            return true;
        }
        else
        {
            $error = 'Unauthorized';
        }
        echo '{"error":"' . $error . '"}';
        exit;
    }

    function fields_erp_product_info($data)
    {
        return $this->get_product_info_from_erp->fields_product_info($data);
    }
    
    function get_all_parts($point, $merchant_num, $warranty, $quantity, $keyword)
    {
        $result = $this->get_product_info_from_erp->get_all_parts($keyword);
        if ($result['success'])
        {
            $result = $this->fech_part_invnetory_new($point, $merchant_num, $warranty, $quantity, $result);
        }
        var_dump($result);
        return $result;
    }

    function get_parts($point, $merchant_num, $warranty, $quantity, $parts_id, $keyword, $sku='', $type_id='', $brand_id='', $model_id='')
    {
        $result = $this->get_product_info_from_erp->get_parts($parts_id, $keyword, $sku, $type_id, $brand_id, $model_id);
        if ($result['success'])
        {
            $result = $this->fech_part_invnetory_new($point, $merchant_num, $warranty, $quantity, $result);
        }
        return $result;
    }

    function fech_part_invnetory_new($point, $merchant_num, $warranty, $quantity, $result)
    {

        //产品处理
        $data_type = 'data';
        if (is_array($result['data']))
        {
            $part_list = $result['data'];
        }
        else
        {
            $part_list = $result['message'];
            $data_type = 'message';
        }
        $whs_code = '';
        $type='';
        echo $point."#".$merchant_num."<br>";
        $whs_code_my = $this->warehouse_list_model->get_whs_by_point($point, $merchant_num);
        $whs_code_dh = $this->warehouse_list_model->get_whs_by_point($point, 0);
        echo $whs_code_my."#".$warranty."#".$whs_code_dh."#<br>";
        if($whs_code_my&&$warranty){
            $whs_code=$whs_code_my;
            $type='merchant';
        }else{
            $whs_code=$whs_code_dh;
            $type='maintain';
        }
        foreach ($part_list as $key => $one_part)
        {
                        $sap_sku = $one_part['sap_sku'];
            $quantity = $this->product_inventory_model->get_available($sap_sku, $whs_code);
            $inventory_quantity[] = array(
                'whs_code' => $whs_code,
                'type' => $type,
                'quantity' => $quantity
            );
            $part_list[$key]['inventory'] = $inventory_quantity;
        }

        if ($data_type == 'data')
        {
            $result['data'] = $part_list;
        }
        else
        {
            $result['message'] = $part_list;
        }

        return $result;
    }

    function fech_part_invnetory($point, $merchant_num, $quantity, $result)
    {
        //本身仓库String
        $whs_code_my = $this->warehouse_list_model->get_whs_by_point($point, $merchant_num);
        //维修仓String
        $whs_code_dh = $this->warehouse_list_model->get_whs_by_point($point, 0);
        //其他商家仓库list
        $whs_code_list = $this->warehouse_list_model->get_point_merchant($point);
        //其他商家仓库Array
        $whs_code_other = array_column($whs_code_list, "whs_code");
        //所有仓库的集合array
        if (!is_array($whs_code_other))
        {
            $whs_code_other = array();
        }
        $whs_all = array_unique(array_merge($whs_code_other, array($whs_code_dh), array($whs_code_my)));
        $data_type = 'data';
        if (is_array($result['data']))
        {
            $part_list = $result['data'];
        }
        else
        {
            $part_list = $result['message'];
            $data_type = 'message';
        }
        foreach ($part_list as $key => $one_part)
        {
            $inventory_quantity = array();
            //默认自己的仓库
            $whs_code = $whs_code_my == '' ? $whs_code_dh : $whs_code_my;
            $type = 'merchant';
            $quantity = 0;
            $sap_sku = $one_part['sap_sku'];
            //查询所有此SKU库存
            $inventory_query = $this->product_inventory_model->get_available_by_array($sap_sku, $whs_all);
            if ($inventory_query)
            {
                $inventory_list_key = array_column($inventory_query, "whs_code");
                $inventory_list_val = array_column($inventory_query, "available");
                $inventory_list = array_combine($inventory_list_key, $inventory_list_val);
                //商家id仓库——无货——维保仓——无货——维修点005仓库+其他商家id编号仓库
                if (!empty($inventory_list[$whs_code_my]) & $inventory_list[$whs_code_my] > $quantity)
                {//商家ID仓库存足够
                    $whs_code = $whs_code_my;
                    $type = 'merchant';
                    $quantity = $inventory_list[$whs_code_my];
                }
                elseif (!empty($inventory_list[$whs_code_dh]) & $inventory_list[$whs_code_dh] > $quantity)
                {//维修仓库存足够
                    $whs_code = $whs_code_dh;
                    $type = 'maintain';
                    $quantity = $inventory_list[$whs_code_dh];
                }
                else
                {
                    /*
                    arsort($inventory_list);
                    list($m_whs_code, $m_quantity) = (reset($inventory_list) ? each($inventory_list) : each($inventory_list));
                    if ($m_quantity > $quantity)
                    {
                        //所有仓最大库存数足够
                        $whs_code = $m_whs_code;
                        $type = 'other_merchant';
                        $quantity = $m_quantity;
                    }
                    else
                    {//库存都不够
                        if (!empty($inventory_list[$whs_code_my]) & $inventory_list[$whs_code_my] > 0)
                        {//商家仓有库存
                            $whs_code = $whs_code_my;
                            $type = 'merchant';
                            $quantity = $inventory_list[$whs_code_my];
                        }
                        elseif (!empty($inventory_list[$whs_code_dh]) & $inventory_list[$whs_code_dh] > 0)
                        {//维修仓有库存
                            $whs_code = $whs_code_dh;
                            $type = 'maintain';
                            $quantity = $inventory_list[$whs_code_dh];
                        }
                        else
                        {
                            unset($inventory_list[$whs_code_my]);
                            unset($inventory_list[$whs_code_dh]);
                            foreach ($inventory_list as $other_one_code => $other_one_qty)
                            {//其他仓有库存
                                if ($other_one_qty > 0)
                                {
                                    $whs_code = $other_one_code;
                                    $type = 'other_merchant';
                                    $quantity = $other_one_qty;
                                    break;
                                }
                            }
                        }
                    }
                     * 
                     */
                    //自己仓库没货 维保也没货 最后还是设定为自己仓库
                    $whs_code = $whs_code_my;
                    $type = 'merchant';
                    $quantity = $inventory_list[$whs_code_my];
                }
                    
            }
            $inventory_quantity[] = array(
                'whs_code' => $whs_code,
                'type' => $type,
                'quantity' => $quantity
            );
            $part_list[$key]['inventory'] = $inventory_quantity;
        }
        if ($data_type == 'data')
        {
            $result['data'] = $part_list;
        }
        else
        {
            $result['message'] = $part_list;
        }

        return $result;
    }

}

?>
